/**
 * 厦门大学计算机专业 | 前华为工程师
 * 专注《零基础学编程系列》  http://lblbc.cn/blog
 * 包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
 * 公众号：蓝不蓝编程
 */
const Router = require('koa-router');

const { CategoryDao } = require('@dao/category');
const { OrderDao } = require('@dao/order');
const { GoodsDao } = require('@dao/goods');
const { Resolve } = require('@lib/helper');
const { Auth } = require('@middlewares/auth');
const res = new Resolve();

const router = new Router({
  prefix: '/mcdonalds'
})

//获取所有分类
router.get('/categories', async (ctx) => {
  const [err, data] = await CategoryDao.queryCategory();
  if (!err) {
    ctx.response.status = 200;
    ctx.body = res.json(data)
  } else {
    ctx.body = res.fail(err)
  }
});

//获取分类下商品
router.get('/goods', async (ctx) => {
  const querystring = require('querystring');
  let request = ctx.request;
  var categoryId = querystring.decode(request.querystring)["categoryId"]
  const [err, data] = await GoodsDao.queryGoodsByCategory(categoryId);
  if (!err) {
    ctx.response.status = 200;
    ctx.body = res.json(data)
  } else {
    ctx.body = res.fail(err)
  }
});

//查询商品详情
router.get('/goods/:goodsId', async (ctx) => {
  let goodsId = ctx.params["goodsId"]
  const [err, data] = await GoodsDao.queryGoods(goodsId);
  if (!err) {
    ctx.response.status = 200;
    ctx.body = res.json(data)
  } else {
    ctx.body = res.fail(err)
  }
});

//搜索
router.get('/goodsBySearch', async (ctx) => {
  const querystring = require('querystring');
  let request = ctx.request;
  var keyword = querystring.decode(request.querystring)["keyword"]
  const [err, data] = await GoodsDao.searchGoods(keyword);
  if (!err) {
    ctx.response.status = 200;
    ctx.body = res.json(data)
  } else {
    ctx.body = res.fail(err)
  }
});

//创建订单
router.post('/orders', new Auth().m, async (ctx) => {
  var userId = ctx.auth.userId
  var simpleCartItemList = ctx.request.body["simpleCartItemList"]
  const [err, data] = await OrderDao.createOrder(userId, simpleCartItemList);
  if (!err) {
    ctx.response.status = 200;
    ctx.body = res.json({ "orderId": data });
  } else {
    ctx.body = res.fail(err);
  }
});

//查询订单列表
router.get('/orders', new Auth().m, async (ctx) => {
  var userId = ctx.auth.userId
  const querystring = require('querystring');
  let request = ctx.request;
  var orderStatus = querystring.decode(request.querystring)["orderStatus"]
  const [err, data] = await OrderDao.queryByStatus(userId, orderStatus);
  if (!err) {
    ctx.response.status = 200;
    ctx.body = res.json(data)
  } else {
    ctx.body = res.fail(err)
  }
});

//查询订单详情
router.get('/orders/:orderId', new Auth().m, async (ctx) => {
  let orderId = ctx.params["orderId"]
  const [err, data] = await OrderDao.queryOrder(orderId);
  if (!err) {
    ctx.response.status = 200;
    ctx.body = res.json(data)
  } else {
    ctx.body = res.fail(err)
  }
});

//删除订单
router.delete('/orders/:orderId', new Auth().m, async (ctx) => {
  let orderId = ctx.params["orderId"]
  const [err, data] = await OrderDao.deleteOrder(orderId);
  if (!err) {
    ctx.response.status = 200;
    ctx.body = res.success('');
  } else {
    ctx.body = res.fail(err);
  }
})



module.exports = router
